package com.df.my_net_game.net.disruptor_test;

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class DisruptorGroup {
    public static void main(String[] args) throws InterruptedException {
        //创建事件处理Disruptor
//        Disruptor<Event> actionDisruptor = new Disruptor<>(new MyEventFactory(), 1024, Executors.defaultThreadFactory());
//        List<Disruptor<Event>> disruptors = new ArrayList<Disruptor<Event>>();
//        for (int i = 1; i <=3; i++) {
//            Disruptor<Event> actionDisruptor = new Disruptor<>(new MyEventFactory(), 1024, new MyThreadFactory(i), ProducerType.SINGLE, new BlockingWaitStrategy());
//            actionDisruptor.handleEventsWith(new EventProcessor(1));
//            disruptors.add(actionDisruptor);
//        }

        //事件处理事件处理器
        Disruptor<Event> actionDisruptor = new Disruptor<>(new MyEventFactory(), 1024, new MyThreadFactory(1), ProducerType.SINGLE, new BlockingWaitStrategy());
//        actionDisruptor.handleEventsWithWorkerPool(new EventProcessor());
        actionDisruptor.handleEventsWith(new EventProcessor(1));
//        actionDisruptor.handleEventsWith(new EventProcessor(2));
//        actionDisruptor.handleEventsWith(new EventProcessor(3));
        actionDisruptor.start();
//        for (Disruptor<Event> disruptor : disruptors) {
//            disruptor.start();
//        }

        for (int i = 0; i < 10; i++) {
//            int index = i%disruptors.size();
//            Disruptor<Event> eventDisruptor = disruptors.get(index);
            RingBuffer<Event> ringBuffer = actionDisruptor.getRingBuffer();
            long next = ringBuffer.next();
            try {
                Event s = ringBuffer.get(next);
                s.message = i+"";
            }finally {
                ringBuffer.publish(next);
            }
        }

        CountDownLatch latch = new CountDownLatch(1);
        latch.await();
    }

    public static class MyThreadFactory implements ThreadFactory {
        int index = 0;
        public MyThreadFactory(int index) {
            this.index = index;
        }
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("LogicHandler-" + index);

            return t;
        }
    }
    public static class Event {

        /**
         * 消息
         */
        private String message;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    public static class MyEventFactory implements EventFactory<Event> {

        @Override
        public Event newInstance() {
            return new Event();
        }
    }

    public static class EventProcessor implements WorkHandler<Event>, EventHandler<Event> {
        int index = 0;
        public EventProcessor(int index) {
            this.index = index;
        }
        @Override
        public void onEvent(Event s, long l, boolean b) throws Exception {
            System.out.println(Thread.currentThread().getName()+" index:"+index+" msg:"+s.getMessage());
        }

        @Override
        public void onEvent(Event s) throws Exception {
            System.out.println(Thread.currentThread().getName()+" index:"+index+" msg:"+s.getMessage());
        }
    }
}
